import time

from selenium.webdriver.common.by import By

topic_text_selector = {  # 多选文字项目·
    "A": '/html/body/div[1]/div[1]/div[1]/div[2]/div[2]/div/div/div[1]/div[1]/div/div/div/div[2]/div/label[1]/span[2]/div/div/div/p',
    "B": '/html/body/div[1]/div[1]/div[1]/div[2]/div[2]/div/div/div[1]/div[1]/div/div/div/div[2]/div/label[2]/span[2]/div/div/div/p',
    "C": '/html/body/div[1]/div[1]/div[1]/div[2]/div[2]/div/div/div[1]/div[1]/div/div/div/div[2]/div/label[3]/span[2]/div/div/div/p',
    "D": '/html/body/div[1]/div[1]/div[1]/div[2]/div[2]/div/div/div[1]/div[1]/div/div/div/div[2]/div/label[4]/span[2]/div/div/div/p'
}
topic_selector = {  # 选择框项
    "A": '//*[@id="exercise_content_area"]/div[1]/div[1]/div/div/div/div[2]/div/label[1]/span[1]/span'
    , "B": '//*[@id="exercise_content_area"]/div[1]/div[1]/div/div/div/div[2]/div/label[2]/span[1]/span'
    , "C": '//*[@id="exercise_content_area"]/div[1]/div[1]/div/div/div/div[2]/div/label[3]/span[1]/span'
    , "D": '//*[@id="exercise_content_area"]/div[1]/div[1]/div/div/div/div[2]/div/label[4]/span[1]/span'
}
topic_tiankong = '//*[@id="exercise_content_area"]/div[1]/div[1]/div/div/div/div[2]/div/div[2]/div'
topic_tiankong1 = '//*[@id="exercise_content_area"]/div[1]/div[1]/div/div/div/div[2]/div/div[2]/div/p'

bt = '//*[@id="exercise_content_area"]/div[2]/div/div/div[2]/button[2]'

submit = '/html/body/div[1]/div[1]/div[1]/div[1]/div[3]/div/button'
enter_submit = '/html/body/div[3]/div/div[3]/button[2]'

def select_topic_question_encapsulation(topic, topic_type, A, B, C, D):
    '''
    封装选择题问题请求
    :param topic:
    :param topic_type:
    :param A:
    :param B:
    :param C:
    :param D:
    :return:
    '''
    topic_data = {"question": f'{topic.text}',
                  "topic_type": f'{topic_type.text}',
                  "A": f'{A.text}',
                  "B": f'{B.text}',
                  "C": f'{C.text}',
                  "D": f'{D.text}'}
    return topic_data


def judge_topic_question_encapsulation(topic, topic_type):
    '''
    封装判断题请求
    :param topic:
    :param topic_type:
    :return:
    '''
    topic_data = {"question": f'{topic.text}',
                  "topic_type": f'{topic_type.text}',
                  "A": '正确',
                  "B": '错误'}
    return topic_data


def driver_func(topic, topic_type, driver):
    '''
    封装请求返回问题字典
    :param topic:
    :param topic_type:
    :param driver:
    :return:
    '''
    topic_data = {}
    if topic_type.text == "单选题" or topic_type.text == "多选题":
        topic_data = select_topic_question_encapsulation(topic,
                                                         topic_type,
                                                         driver.find_element(By.XPATH, topic_text_selector.get("A")),
                                                         driver.find_element(By.XPATH, topic_text_selector.get("B")),
                                                         driver.find_element(By.XPATH, topic_text_selector.get("C")),
                                                         driver.find_element(By.XPATH, topic_text_selector.get("D")))
    elif topic_type.text == "判断题":
        topic_data = judge_topic_question_encapsulation(topic, topic_type)
    elif topic_type.text == "填空题":
        topic_data = {
            "question": f'{topic.text}',
            "topic_type": f'{topic_type.text}'
        }
    return topic_data


def select_options(driver, option):
    '''
    选择选项
    :param driver:
    :param option:
    :return:
    '''
    for i in topic_selector.keys():
        if i in option.content:
            a = driver.find_element(By.XPATH, topic_selector.get(i))
            a.click()


def tian_kong(driver, ans):
    # option = driver.find_element(By.XPATH, topic_tiankong)
    # option.click()
    option1 = driver.find_element(By.XPATH, topic_tiankong1)
    option1.clear()
    option1.send_keys(ans.content)
    time.sleep(0.5)
    # 输入完之后点一下填空题
    next_topics = driver.find_element(By.XPATH,bt)
    next_topics.click()
    # fd = driver.find_element(By.XPATH,'//*[@id="exercise_content_area"]/div[1]/div[1]/div/div/div/div[1]/div[1]/div[1]/div[2]')
    # fd.click()
